package com.everflourish.yeah100.ui;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.everflourish.yeah100.R;

public class ExitPopup extends PopupWindow {

    private String mMsg;
    private View mView;
    private TextView mTitleTv;
    private ImageView mCameraIv, mAlbumIv;
    private CircleImageView mImageView;

    public interface OnClickListener {
        public void onClick(ExitPopup exitPopup);
    }

    private OnClickListener confirmListener, neutralityListener,
            cancelListener;

    public ExitPopup(final Activity act, String msg) {
        mMsg = msg;
        mView = View.inflate(act, R.layout.popup_exit, null);
        mCameraIv = (ImageView) mView.findViewById(R.id.popup_camera_iv);
        mAlbumIv = (ImageView) mView.findViewById(R.id.popup_album_iv);
        mTitleTv = (TextView) mView.findViewById(R.id.popup_title_tv);
        // 设置SelectPicPopupWindow的View
        this.setContentView(mView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        WindowManager.LayoutParams att = act.getWindow().getAttributes();
        att.alpha = 0.4f;
        act.getWindow().setAttributes(att);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams att = act.getWindow()
                        .getAttributes();
                att.alpha = 1f;
                act.getWindow().setAttributes(att);
            }
        });
    }

    public void setMsg(String msg) {
        if (msg == null) {
            mTitleTv.setVisibility(View.GONE);
        } else {
            mTitleTv.setVisibility(View.VISIBLE);
            mTitleTv.setText(msg);
        }
    }

    public void setImageView(Bitmap bm) {
        mImageView.setVisibility(View.VISIBLE);
        mImageView.setImageBitmap(bm);
    }

    /**
     * 设置相机按钮监听
     *
     * @param onClickListener 点击监听
     */
    public void setCameraListener(OnClickListener onClickListener) {
        if (onClickListener == null) {
            mCameraIv.setVisibility(View.GONE);
        } else {
            mCameraIv.setVisibility(View.VISIBLE);
        }
        this.confirmListener = onClickListener;
    }

    /**
     * 设置相册按钮的监听
     *
     * @param onClickListener 点击监听
     */
    public void setAlbumListener(OnClickListener onClickListener) {
        if (onClickListener == null) {
            mAlbumIv.setVisibility(View.GONE);
        } else {
            mAlbumIv.setVisibility(View.VISIBLE);
        }
        this.cancelListener = onClickListener;
    }

    public void show(View parent, Activity activity) {
//        mTitleTv.setText(mMsg);
        if (confirmListener != null) {
            mCameraIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirmListener.onClick(ExitPopup.this);
                }
            });
        } else {
            mCameraIv.setVisibility(View.GONE);
        }
        if (cancelListener != null) {
            mAlbumIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancelListener.onClick(ExitPopup.this);
                }
            });
        } else {
            mAlbumIv.setVisibility(View.GONE);
        }
        super.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

}